-- Создана русскоязычным telegram-каналом https://t.me/official_jieshuo_ru
if RFirst then
    RFirst = 1
else
    RFirst = 2
end

if RFirst == 2 then
    RCount = 1
end

if RFirst == 1 then
    RCount = RCount + 1
end

if RCount > 2 then
    RCount = 1
end

switch RCount
    case 1
        require "import"
        import "android.content.Context"
        import "android.os.Vibrator"
        import "java.io.File"
        import "java.text.SimpleDateFormat"
        import "android.media.MediaRecorder"
        import "android.media.MediaScannerConnection"
        import "android.content.ContentValues"
        import "android.provider.MediaStore"
        local folderPath = "/storage/emulated/0/Recordings"
        local folder = File(folderPath)
        if not folder.exists() then
            local success = folder.mkdirs()
            if success then
                service.asyncSpeak("Папка создана: " .. folderPath)
            else
                service.asyncSpeak("Не удалось создать папку по пути: " .. folderPath)
            end
        end
        local isRecording = false
        local folderName = "Recordings"
        local folderPath = "/storage/emulated/0/" .. folderName
        local formatDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        local formatTime = SimpleDateFormat("HH_mm_ss", Locale.getDefault())
        local currentDate = formatDate.format(Date())
        local currentTime = formatTime.format(Date())
        local outputFile = folderPath .. "/recorded_audio_" .. currentDate .. "_" .. currentTime .. ".ogg"
        function stopRecording()
            if isRecording then
                mediaRecorder.stop()
                mediaRecorder.release()
                mediaRecorder = nil
                isRecording = false
                MediaScannerConnection.scanFile(this, {outputFile}, nil, nil)
                local values = ContentValues()
                values.put(MediaStore.MediaColumns.DATA, outputFile)
                values.put(MediaStore.MediaColumns.TITLE, "Аудиозапись")
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg")
                this.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values)
            end
        end
        local function startRecording()
            if not isRecording then
                mediaRecorder = MediaRecorder()
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.OGG)
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.OPUS)
                mediaRecorder.setAudioEncodingBitRate(320000)
                mediaRecorder.setAudioSamplingRate(48000)
                mediaRecorder.setAudioChannels(2)
                outputFile = folderPath .. "/recorded_audio_" .. currentDate .. "_" .. currentTime .. ".ogg"
                mediaRecorder.setOutputFile(outputFile)
                mediaRecorder.prepare()
                mediaRecorder.start()
                isRecording = true
            end
        end
        startRecording()
        local context = activity or service
        local vibrator = context.getSystemService(Context.VIBRATOR_SERVICE)
        if vibrator.hasVibrator() then
            vibrator.vibrate(100)
        end
        return true
    case 2
        stopRecording()
        service.asyncSpeak("Запись завершена")
        require "import"
        import "android.content.Context"
        import "android.os.Vibrator"
        local context = activity or service
        local vibrator = context.getSystemService(Context.VIBRATOR_SERVICE)
        if vibrator.hasVibrator() then
            vibrator.vibrate(200)
        end
        return true
end
return true